home *** CD-ROM | disk | FTP | other *** search
- Path: keats.ugrad.cs.ubc.ca!not-for-mail
- From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
- Newsgroups: comp.lang.c
- Subject: Re: pointers
- Date: 25 Feb 1996 14:09:05 -0800
- Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
- Message-ID: <4gqmm1INNmej@keats.ugrad.cs.ubc.ca>
- References: <4gqh8g$bo4@news.bu.edu>
- NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
-
- In article <4gqh8g$bo4@news.bu.edu>, wai yip <lachesis@cs.bu.edu> wrote,
- surprisingly on topic for the newsgroup:
-
- >can someone who knows a lot about pointers help me with this
- >
- >int i=3,*p;
- >
- >with the above declaration, what would the bottom lines do?
-
- >*p=&i;
-
- (incorrectly) tries to assign the address of integer i to the
- storage (of type int) pointed at by (the uninitialized pointer) p.
-
- You have comitted two errors. First, you are using a pointer that has not been
- initialized to point anywhere. At best, if the above declaration appears
- outside of any function so p is initialized for you as the null pointer.
- Secondly, you are assigning incompatible types.
-
- What the above does is undefined according to the C standard.
-
- >p=i;
-
- Incorrectly assigns an integer to a pointer variable. This is possible with
- an appropriate cast if you know what you are doing, but hopelessly makes your
- code non-portable. If you need a piece of storage that can alternately hold a
- pointer or an integer (but never both at once), you can use a union.
-
- >*p=i;
-
- In this case, the types _are_ assignment compatible. You trying to assign the
- value of integer i to the integer pointed at by p. Unfortunately, p has not
- been initialized to point to an integer variable.
-
- >p=&i;
-
- This is the only correct statement so far. This will cause p to hold the
- address of integer i.
-
- >please answer me through email because i don't usually read newsgroups.
-
- One usually pays for private tutors. I will send you a cc: but I would
- recommend that you check the responses on the newsgroup. You might learn a
- thing or two from people who are otherwise reluctant to respond in e-mail.
- --
-
-